package com.leyou.auth.service.impl;

import com.leyou.auth.client.UserClient;
import com.leyou.auth.pojo.UserInfo;
import com.leyou.auth.properties.JwtProperties;
import com.leyou.auth.service.AuthService;
import com.leyou.auth.utils.JwtUtils;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.user.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserClient userClient;
    @Autowired
    private JwtProperties jwtProperties;
    @Override
    public String anthentication(String username, String password) {
       try{
        //调用微服务查询数据
           User user = userClient.queryUser(username, password);
         //  System.out.println(userClient.QueryUserByUserNameByPassWord(username, password));
      // User user = userClient.QueryUserByUserNameByPassWord(username, password);
        System.out.println(user);
        if(user==null){
            return null;
        }
        UserInfo userInfo = new UserInfo(user.getId(), user.getUsername());
           System.out.println("userInfo"+userInfo);
           System.out.println(jwtProperties.getPrivateKey());
           System.out.println( jwtProperties.getExpire());
        //生成Token
        String token = JwtUtils.generateToken(userInfo, jwtProperties.getPrivateKey(), jwtProperties.getExpire());
        return token;
    } catch (Exception e) {
        log.error("【授权中心】用户名和密码错误，用户名：{}", username,e);
        throw new LyException(ExceptionEnum.INVALID_USERNAME_PASSWORD);
    }
}
}
